package com;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
import java.nio.charset.Charset;
import java.text.DateFormat;
import java.text.MessageFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Response implements Runnable {
	private Socket s;
	private byte[] buffer;
	private String path;
	private Integer idSession;


	private enum ResultMsg {
		OK, ERROR
	}

	private String typeOfContent;

	public Socket getS() {
		return s;
	}

	public void setS(Socket s) {
		this.s = s;
	}

	public byte[] getBuffer() {
		return buffer;
	}

	public void setBuffer(byte[] buffer) {
		this.buffer = buffer;
	}

	@Override
	public void run() {
		if (path.endsWith(".jpg ") || path.endsWith(".ico")) {
			typeOfContent = "image/jpeg; ";
		} else {
			typeOfContent = "text/html; ";
		}
		// typeOfContent="text/html ";

		System.out.println(typeOfContent);

	
		ResultMsg resultMsg;
		String code;
		if (buffer != null) {
			resultMsg = ResultMsg.OK;
			code = "200";
		} else {
			resultMsg = ResultMsg.ERROR;
			code = "404";
			try {
				File errorPage = new File(".\\MyPrograms\\404.html");
				FileInputStream fis = new FileInputStream(errorPage);
				int size = (int) errorPage.length();
				byte[] data = new byte[size];
				fis.read(data);
				buffer = data;
				fis.close();
			} catch (IOException e) {
			
				e.printStackTrace();
			}
			// content = new String(data);
		}
		// String content = new String(buffer);
		DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
		Date date = new Date();
		String nameOfServer = "Server: Dp31-Java/";
		String time = dateFormat.format(date).toString();
		String header1 = MessageFormat.format("Http/1.1 {0} {1}\r\n", code,
				resultMsg);
		
		String name="jidsession=";
		
		String header2 = MessageFormat.format("{0} {1} \r\n", nameOfServer,
				time);
		String header3 = MessageFormat.format("Content-Type: {0}",
				typeOfContent);
		String header4 = "Content-Length: " + buffer.length + "\r\n"
				+ "Connection: close\r\n\r\n";

		String header5 = MessageFormat.format("Set-Cookie:{0}{1}\r\n",name,SessionCollection.Instance().getSession(idSession).getId());
	
		String response = header1 + header2 + header5 + header3 + header4;
		System.out.println("itogo "+response);
		try {
			OutputStream os = getS().getOutputStream();
			os.write(response.getBytes(Charset.forName("UTF-8")));
			os.write(buffer);
			os.flush();
			getS().close();
		} catch (IOException e) {
		
			e.printStackTrace();
		}

	}

	
	public String getTypeOfContent() {
		return typeOfContent;
	}

	
	public Response(Socket s, byte[] buffer, String path, Integer n) {
		this.s = s;
		this.buffer = buffer;
		this.path = path;
		this.idSession=n;
	}

	public void setTypeOfContent(String typeOfContent) {
		this.typeOfContent = typeOfContent;
	}



}
